package com.awei.study.interview.lock;

import lombok.SneakyThrows;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author liweilong
 * @Date 2024/11/27 21:27
 */
public class CountDownLatchTest {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(5);
        CountDownLatch latch = new CountDownLatch(5);
        for (int i = 0; i < latch.getCount(); i++) {
            final int x = i + 1;
            pool.submit(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    System.out.println("第"+ x + "条线程任务开始执行");
                    Thread.sleep(x * 1000L);
                    System.out.println("第"+ x + "条线程任务结束执行");
                    latch.countDown();
                }
            });
        }
        latch.await();
        System.out.println(latch);
    }
}
